package com.htbb.common.util;

import java.text.DecimalFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 
 * 字符串相关工具类
 * @author wangzhicheng
 *
 */
public class StringUtil {

	// 首字母转小写
	public static String toLowerCaseFirstOne(String s) {
		if (Character.isLowerCase(s.charAt(0)))
			return s;
		else
			return (new StringBuilder()).append(Character.toLowerCase(s.charAt(0))).append(s.substring(1)).toString();
	}

	// 首字母转大写
	public static String toUpperCaseFirstOne(String s) {
		if (Character.isUpperCase(s.charAt(0)))
			return s;
		else
			return (new StringBuilder()).append(Character.toUpperCase(s.charAt(0))).append(s.substring(1)).toString();
	}
	
	/**
	 * 保留1位小数
	 */
	public static final float getOneFloat(float value)  {
		DecimalFormat fnum = new DecimalFormat("##0.0"); 
		return Float.parseFloat(fnum.format(value)); 
	}
	
	/**
	 * 保留2位小数
	 */
	public static final float getTwoFloat(float value)  {
		DecimalFormat fnum = new DecimalFormat("##0.00"); 
		return Float.parseFloat(fnum.format(value)); 
	}
	/**
	 * 保留2位小数
	 */
	public static final double getTwoDouble(double value)  {
		DecimalFormat fnum = new DecimalFormat("##0.00"); 
		return Double.parseDouble(fnum.format(value)); 
	}
	/**
	 * 保留4位小数
	 */
	public static final float getFourFloat(float value)  {
		DecimalFormat fnum = new DecimalFormat("##0.0000"); 
		return Float.parseFloat(fnum.format(value)); 
	}
	
	/**
	 * 去掉中文字符
	 * @param str
	 * @return
	 */
	public static String removeZh(String str){
		String regEx = "[\\u4e00-\\u9fa5]";
		Pattern p = Pattern.compile(regEx);
		Matcher m = p.matcher(str);
		return m.replaceAll("");
	}
	
	/**
	 * 留下中文开头的字符串
	 * @param str
	 * @return
	 */
	public static String subFistZh(String str){
		if(str == null || str ==""){
			return str;
		}
		String resultstr = str;
		String regEx = "[\\u4e00-\\u9fa5]";
		Pattern p = Pattern.compile(regEx);
		for(int i=1;i<str.length();i++){
			Matcher m = p.matcher(str.substring(i-1, i));
			if(m.matches()){
				resultstr = str.substring(0, i);
			}else{
				break;
			}
		}
		return resultstr;
	}
	
	/**
	 * 去掉中文结尾的字符,但是如果全是中文则不处理
	 * @param str
	 * @return
	 */
	public static String removelastZh(String str){
		String oristr = str;
		if(str == null || str ==""){
			return str;
		}
		
		boolean notallzh = true; //是否全部为中文
		String regEx = "[\\u4e00-\\u9fa5]";
		Pattern p = Pattern.compile(regEx);
		while(str.substring(0, str.length()-1).length() > 0){
			Matcher m = p.matcher(str.substring(str.length()-1, str.length()));
			if(m.matches()){
				str = str.substring(0, str.length()-1);
			}else{
				notallzh = false;
				break;
			}
			
		}
		if(notallzh){
			return oristr;
		}
		return str;
	}
	
	/**
	 * 判断车架号是不是L，不是L则只留下中文，否则去掉中文结尾的字符，并且去掉牌
	 * @param str,cjh车架号
	 * @return
	 */
	public static String zhFilter(String str,String cjh){
		if(str == null || str ==""){
			return str;
		}
		if(cjh == null || cjh ==""){
			return removelastZh(str);
		}
		str = str.replace("牌", "");
		if(cjh.startsWith("L") || cjh.startsWith("l")){
			return removelastZh(str);
		}else{
			return subFistZh(str);
		}
	}
	
	/**
	 * 根据身份证获取性别 1 男 2 女 9 未说明
	 * @param idnumber
	 * @return
	 */
	public static Integer getSex(String idnumber){
		if(idnumber.length() == 15){
			 Integer num = Integer.parseInt(idnumber.substring(idnumber.length()-1, idnumber.length()));
			 return num%2==0?2:1;
		}else if(idnumber.length() == 18){
			 Integer num = Integer.parseInt(idnumber.substring(idnumber.length()-2, idnumber.length()-1));
			 return num%2==0?2:1;
		}
		return 9;
	}
	
	/**
	 * 根据身份证获取年龄
	 * @param idnumber
	 * @return
	 */
	public static Integer getAge(String idnumber){
		 Integer num = Integer.parseInt(idnumber.substring(6, 10));
		 return Calendar.getInstance().get(Calendar.YEAR)- num;
	}
	
	/**
	 * 根据身份证获取年龄
	 * @param idnumber
	 * @return
	 */
	public static boolean hasEnglish(String str){
		Pattern p = Pattern.compile("[a-zA-z]");
        if(p.matcher(str).find()){
        	return true;
        }else{
        	return false;
        }
	}
	
	/**
	 * 加权随机
	 * @param servers
	 * @return
	 */
	public static String getRandomByRate(Map<String, Integer> servers){  
        if(servers == null || servers.size() == 0) 
        	return null;  
          
        Integer sum = 0;  
        Set<Map.Entry<String, Integer>> entrySet = servers.entrySet();  
        Iterator<Map.Entry<String, Integer>> iterator = entrySet.iterator();  
        while(iterator.hasNext()){  
            sum += iterator.next().getValue();  
        }  
        Integer rand = new Random().nextInt(sum) + 1;  
        for(Map.Entry<String, Integer> entry : entrySet){  
            rand -= entry.getValue();  
            if(rand <=0){  
                return entry.getKey();  
            }  
        }  
          
        return null;  
    }  
	
	public static void main(String[] args) {
//		System.out.println(StringUtil.getTwoFloat(Float.parseFloat("32500.0")));
//		System.out.println(StringUtil.getTwoFloat(Float.parseFloat("32500.0"))*100+"");
//		System.out.println(Math.round(StringUtil.getTwoFloat(Float.parseFloat("32500.0"))*100)+"");
//		System.out.println(Long.valueOf(StringUtil.getTwoFloat(Float.parseFloat("32500.0"))*100+""));
//		System.out.println(zhFilter("速腾FV7206AT轿车","LFV3A21K773053009"));
//		System.out.println(getSex("432524199009220634"));
//		System.out.println(getAge("432524199009220634"));
//		System.out.println(hasEnglish("43d2524199009220634"));
		
		Map<String, Integer> servers = new HashMap<String, Integer>();  
        servers.put("192.168.20.101", 30);  
        servers.put("192.168.20.103", 10);  
		System.out.println(getRandomByRate(servers));
		System.out.println(getRandomByRate(servers));
		System.out.println(getRandomByRate(servers));
		System.out.println(getRandomByRate(servers));
	}
	
}
